package com.ht.dsp.test;

import _61970.wires.BusbarSection;
import _61970.wires.SynchronousMachine;
import com.ht.aems.ntp.CIMConstants;
import com.ht.aems.util.RtDataHandler;
import com.ht.aems.util.SerializableUtil;
import com.ht.dsp.DspSysModel;
import com.ht.memcached.MemCachedConfigurator;
import devmodel.MapObject;
import hhh.utils.FileUtil;
import rtdb.RtDataHandler_MemCached;
import rtdb.cim.CimKeys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Dong Shufeng
 * Date: 11-8-25
 */
public class CimResourceExample implements CimKeys, CIMConstants {
    public static void main(String[] args) {
        String f = FileUtil.getRootDirectory() + "/_config/memcache.properties";
        System.out.println("Load memcached config file : " + f);
        MemCachedConfigurator.config(f);
        DspSysModel.INSTANCE.setRtDataHandler(new RtDataHandler_MemCached());

        System.out.println("=========== test start ===================");
        RtDataHandler rtDataHandler = DspSysModel.INSTANCE.getRtDataHandler();
        String ids = (String) rtDataHandler.get(CimKeys.KEY_SUBSTATION_IDS);

        if (ids == null) {
            System.out.println("There is no substations in rtdb.");
        } else {
            List<String> substationIds = SerializableUtil.parseList(ids);
            Map<String, Object> m = rtDataHandler.getMulti(substationIds.toArray(new String[]{}));
            if (m == null) {
                System.out.println("There is no substation in rtdb.");
                return;
            }
            if (m.size() != substationIds.size())
                System.out.println("Wrong substations' number got from rtdb.");
            Map<String, MapObject> substations = new HashMap<String, MapObject>(m.size());
            for (Object obj : m.values()) {
                MapObject mapObj = MapObject.createObj(obj.toString());
                if (mapObj == null)
                    System.out.println("Fail to decode map object.");
                else
                    substations.put(mapObj.getId(), mapObj);
            }
            for (String substationId : substations.keySet()) {
                String equip = (String) rtDataHandler.get(substationId + SUFFIX_CONTAINED_EQUIPMENT);
                MapObject substation = substations.get(substationId);
                if (equip == null)
                    System.out.println("No equipment is found in rtdb of station id:" + substationId + " name: " + substation.getName());
                else {
                    List<String> equipIds = SerializableUtil.parseList(equip);
                    m = rtDataHandler.getMulti(equipIds.toArray(new String[]{}));
                    if (m == null) {
                        System.out.println("No equipment can be got in rtdb of station id:" + substationId + " name: " + substation.getName());
                        continue;
                    }
                    Map<String, MapObject> stationEquips = new HashMap<String, MapObject>();
                    System.out.println("============ substaion " + substations.get(substationId) + " ==========");
                    for (Object obj : m.values()) {
                        MapObject mapObj = MapObject.createObj(obj.toString());
                        if (mapObj == null)
                            System.out.println("Fail to decode map object.");
                        else
                            stationEquips.put(mapObj.getId(), mapObj);
                        System.out.println(mapObj);
                    }
                    //get measurement of generator mw and mvar measurements and bus' voltage measurements
                    for (MapObject obj : stationEquips.values()) {
                        if (obj.getType().equals(SynchronousMachine.class.getSimpleName())) {
                            Object s = rtDataHandler.get(obj.getId() + SUFFIX_CONTAINED_ANALOG);
                            if (s == null)
                                continue;
                            List<String> l = SerializableUtil.parseList(s.toString());
                            for (String measId : l) {
                                String s2 = rtDataHandler.get(measId).toString();
                                MapObject measure = MapObject.createObj(s2);
                                String typeId = measure.getProperty("MeasurementType");
                                String s3 = rtDataHandler.get(typeId).toString();
                                MapObject measType = MapObject.createObj(s3);
                                if (measType.getName().equals(TYPE_THREE_PHASE_ACTIVE_POWER)) {
                                    System.out.println("active power measure generator name: " + obj.getName() + " measure name: " + measure.getName());
                                } else if (measType.getName().equals(TYPE_THREE_PHASE_REACTIVE_POWER)) {
                                    System.out.println("reactive power measure generator name: " + obj.getName() + " measure name: " + measure.getName());
                                }
                            }
                        } else if (obj.getType().equals(BusbarSection.class.getSimpleName())) {
                            Object s = rtDataHandler.get(obj.getId() + SUFFIX_CONTAINED_ANALOG);
                            if (s == null)
                                continue;
                            List<String> l = SerializableUtil.parseList(s.toString());
                            for (String measId : l) {
                                String s2 = rtDataHandler.get(measId).toString();
                                MapObject measure = MapObject.createObj(s2);
                                String typeId = measure.getProperty("MeasurementType");
                                String s3 = rtDataHandler.get(typeId).toString();
                                MapObject measType = MapObject.createObj(s3);
                                if (measType.getName().equals(TYPE_LINE_TO_LINE_VOLTAGE))
                                    System.out.println("voltage measure bus name: " + obj.getName() + " measure name: " + measure.getName());
                                else if (measType.getName().equals(TYPE_VOLTAGE))
                                    System.out.println("voltage measure bus name: " + obj.getName() + " measure name: " + measure.getName());
                            }
                        }
                    }
                    System.out.println("============ substaion " + substations.get(substationId) + " end ======");
                }
            }
        }
    }
}
